package com.tledu.springboot01.dao.impl;

import com.tledu.springboot01.dao.IUserDao;
import com.tledu.springboot01.model.User;
import com.tledu.springboot01.rowmapper.UserRowMapper;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
@AllArgsConstructor
public class UserDaoImpl implements IUserDao {
    private final JdbcTemplate jdbcTemplate;

    @Override
    public List<User> list() {
        return jdbcTemplate.query("select * from user", new UserRowMapper());
    }

    @Override
    public int add(User user) {
        return jdbcTemplate.update("insert into user " +
                        "(username, password, nickname, age, birthday, school_name, avatar) " +
                        "value (?,?,?,?,?,?,?)",
                user.getUsername(),
                user.getPassword(),
                user.getNickname(),
                user.getAge(),
                user.getBirthday(),
                user.getSchoolName(),
                user.getAvatar()
        );
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        List<Object[]> args = new ArrayList<>();
        for (Integer id : ids) {
            args.add(new Object[]{id});
        }
        int[] numList= jdbcTemplate.batchUpdate("delete from user where id = ?",args);
        int sum = 0;
        for (int i : numList) {
            sum += i;
        }
        return sum;
    }
}
